package com.carlinfo.userscenter.users.service.impl;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.carlinfo.common.service.BaseService;
import com.carlinfo.common.util.PageInfoUtil;
import com.carlinfo.userscenter.users.dao.IAAdminsDao;
import com.carlinfo.userscenter.users.dao.IARoleDao;
import com.carlinfo.userscenter.users.dao.impl.AAdminsDaoImpl;
import com.carlinfo.userscenter.users.dao.impl.ARoleDaoImpl;
import com.carlinfo.userscenter.users.pojo.AAdmins;
import com.carlinfo.userscenter.users.pojo.ARole;
import com.carlinfo.userscenter.users.pojo.ARoleMenu;
import com.carlinfo.userscenter.users.service.IAuthService;
import com.carlinfo.userscenter.users.service.IUsersService;

/**
 * usersService的实现类
 * 
 * @author wangshMac
 *
 */
public class UsersServiceImpl extends BaseService implements IUsersService
{
	/**
	 * =左边是父类
	 * =右边是子类
	 * 父类引用指向子类对象
	 */
	private IARoleDao roleDao = new ARoleDaoImpl();
	private IAAdminsDao adminsDao = new AAdminsDaoImpl();
	
	private IAuthService authService;
	
	
	/**
	 * {
			// 响应码/
			"code":"0",
			//响应码的描述 
			"info":"成功",
			"data":
			{
				//id:
				"id":"",
				//sql语句影响的条数
				"effect":""
			}
		}
	 */
	@Override
	public JSONObject saveOneRoleService(ARole role)
	{
		JSONObject resultJSON = new JSONObject() ; 
		/* dao返回值指的是执行sql语句影响力的条数 */
		int res =  this.roleDao.saveOneDao(role);
		if(res > 0 )
		{
			/* 添加成功 */
			JSONObject dataJSON = new JSONObject() ; 
			/* 在json中,id是整数 */
			dataJSON.put("id", role.getId() + "");
			/* ""+任何类型,结果都是字符串类型 */
			dataJSON.put("effect", res + "");
			/* 将resultJSON和dataJSON关联起来 */
			resultJSON.put("data", dataJSON);
			
			resultJSON.put("code", "0");
			resultJSON.put("info", "添加成功");
		}else
		{
			/* 添加失败 */
			resultJSON.put("code", "1");
			resultJSON.put("info", "添加失败");
		}
		return resultJSON ; 
	}

	@Override
	public JSONObject updateOneRoleService(ARole role)
	{
		JSONObject resultJSON = new JSONObject() ; 
		/* dao返回值指的是执行sql语句影响力的条数 */
		int res =  this.roleDao.updateOneDao(role);
		if(res > 0 )
		{
			/* 添加成功 */
			JSONObject dataJSON = new JSONObject() ; 
			/* ""+任何类型,结果都是字符串类型 */
			dataJSON.put("effect", res + "");
			/* 将resultJSON和dataJSON关联起来 */
			resultJSON.put("data", dataJSON);
			
			resultJSON.put("code", "0");
			resultJSON.put("info", "更新成功");
		}else
		{
			/* 添加失败 */
			resultJSON.put("code", "1");
			resultJSON.put("info", "更新失败");
		}
		return resultJSON ; 
	}

	@Override
	public JSONObject deleteOneRoleService(ARole role)
	{
		JSONObject resultJSON = new JSONObject() ; 
		/* dao返回值指的是执行sql语句影响力的条数 */
		int res =  this.roleDao.deleteOneDao(null);
		if(res > 0 )
		{
			/* 添加成功 */
			JSONObject dataJSON = new JSONObject() ; 
			/* ""+任何类型,结果都是字符串类型 */
			dataJSON.put("effect", res + "");
			/* 将resultJSON和dataJSON关联起来 */
			resultJSON.put("data", dataJSON);
			
			resultJSON.put("code", "0");
			resultJSON.put("info", "删除成功");
		}else
		{
			/* 添加失败 */
			resultJSON.put("code", "1");
			resultJSON.put("info", "删除失败");
		}
		return resultJSON ; 
	}

	@Override
	public ARole findOneRoleService(Map<String, Object> condMap)
	{
		ARole role = this.roleDao.selectOneDao(condMap);
		if("true".equalsIgnoreCase(condMap.get("extend") + ""))
		{
			Map<String,Object> extMap = new HashMap<String, Object>();
			/* 为角色菜单赋值 */
			extMap.clear();
			extMap.put("id", role.getId());
			List<ARoleMenu> roleMenuList = this.authService.findCondListRoleMenuService(null, condMap);
			role.setRoleMenuList(roleMenuList);
		}
		return role ; 
	}

	@Override
	public List<ARole> findCondListRoleService(PageInfoUtil pageInfoUtil ,Map<String, Object> condMap)
	{
		List<ARole> roleList = this.roleDao.selectListDao(pageInfoUtil,condMap);
		if("true".equalsIgnoreCase(condMap.get("extend") + ""))
		{
			Map<String,Object> extMap = new HashMap<String, Object>();
			
			for (Iterator iterator = roleList.iterator(); iterator.hasNext();)
			{
				ARole role = (ARole) iterator.next();
				
				/* 为角色菜单赋值 */
				extMap.clear();
				extMap.put("id", role.getId());
				List<ARoleMenu> roleMenuList = this.authService.findCondListRoleMenuService(null, condMap);
				role.setRoleMenuList(roleMenuList);
			}
		}
		return roleList ; 
	}
	
	/**
	 * {
			// 响应码/
			"code":"0",
			//响应码的描述 
			"info":"成功",
			"data":
			{
				//id:
				"id":"",
				//sql语句影响的条数
				"effect":""
			}
		}
	 */
	@Override
	public JSONObject saveOneAdminsService(AAdmins admins)
	{
		JSONObject resultJSON = new JSONObject() ; 
		/*
		 * 邮箱不能重复
		 * */
		/* 根据邮箱查询用户 */
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("email", admins.getEmail());
		AAdmins adminsRes = this.findOneAdminsService(condMap);
		if(adminsRes != null)
		{
			/* 邮箱已经存在,请更换邮箱 */
			resultJSON.put("code", "2");
			resultJSON.put("info", "邮箱已经存在,请更换邮箱");
			return resultJSON ; 
		}
		
		/* dao返回值指的是执行sql语句影响力的条数 */
		int res =  this.adminsDao.saveOneDao(admins);
		if(res > 0 )
		{
			/* 添加成功 */
			JSONObject dataJSON = new JSONObject() ; 
			/* 在json中,id是整数 */
			dataJSON.put("id", admins.getId() + "");
			/* ""+任何类型,结果都是字符串类型 */
			dataJSON.put("effect", res + "");
			/* 将resultJSON和dataJSON关联起来 */
			resultJSON.put("data", dataJSON);
			
			resultJSON.put("code", "0");
			resultJSON.put("info", "添加成功");
		}else
		{
			/* 添加失败 */
			resultJSON.put("code", "1");
			resultJSON.put("info", "添加失败");
		}
		return resultJSON ; 
	}
	
	@Override
	public JSONObject saveOneAdminsService(AAdmins admins, Map<String, Object> condMap)
	{
		/* 先处理对象,保存到数据库 */
		JSONObject resultJSON = this.saveOneAdminsService(admins);
		/* 然后再处理图片
		 * 直接从Map中取值,在使用的时候,一定要先放值
		 *  */
		String fileName = condMap.get("fileName") + "";
		/* 只有保存成功的时候才更新头像 */
		if(condMap.get("fileName") != null && !"".equalsIgnoreCase(fileName) && "0".equalsIgnoreCase(resultJSON.getString("code")))
		{
		}
		return resultJSON;
	}

	@Override
	public JSONObject updateOneAdminsService(AAdmins admins)
	{
		JSONObject resultJSON = new JSONObject() ; 
		/* dao返回值指的是执行sql语句影响力的条数 */
		int res =  this.adminsDao.updateOneDao(admins);
		if(res > 0 )
		{
			/* 添加成功 */
			JSONObject dataJSON = new JSONObject() ; 
			/* ""+任何类型,结果都是字符串类型 */
			dataJSON.put("effect", res + "");
			/* 将resultJSON和dataJSON关联起来 */
			resultJSON.put("data", dataJSON);
			
			resultJSON.put("code", "0");
			resultJSON.put("info", "更新成功");
		}else
		{
			/* 添加失败 */
			resultJSON.put("code", "1");
			resultJSON.put("info", "更新失败");
		}
		return resultJSON ; 
	}

	@Override
	public JSONObject deleteOneAdminsService(AAdmins admins)
	{
		JSONObject resultJSON = new JSONObject() ; 
		/* dao返回值指的是执行sql语句影响力的条数 */
		int res =  this.adminsDao.deleteOneDao(null);
		if(res > 0 )
		{
			/* 添加成功 */
			JSONObject dataJSON = new JSONObject() ; 
			/* ""+任何类型,结果都是字符串类型 */
			dataJSON.put("effect", res + "");
			/* 将resultJSON和dataJSON关联起来 */
			resultJSON.put("data", dataJSON);
			
			resultJSON.put("code", "0");
			resultJSON.put("info", "删除成功");
		}else
		{
			/* 添加失败 */
			resultJSON.put("code", "1");
			resultJSON.put("info", "删除失败");
		}
		return resultJSON ; 
	}

	@Override
	public AAdmins findOneAdminsService(Map<String, Object> condMap)
	{
		AAdmins admins =  this.adminsDao.selectOneDao(condMap);
		/* 关联关系的开关 */
		if("true".equalsIgnoreCase(condMap.get("extend") + ""))
		{
			/* 查询扩展表的条件 */
			Map<String, Object> extendMap = new HashMap<String, Object>();
			/* 根据id查询角色 
			 * 是通过管理员获取角色的id
			 * */
			extendMap.put("id", admins.getRoleId()) ;
			ARole role = this.findOneRoleService(extendMap);
			admins.setRole(role);
		}
		return admins ; 
	}

	@Override
	public List<AAdmins> findCondListAdminsService(PageInfoUtil pageInfoUtil ,Map<String, Object> condMap)
	{
		List<AAdmins> resultList =  this.adminsDao.selectListDao(pageInfoUtil,condMap);
		
		/* 关联关系的开关 */
		if("true".equalsIgnoreCase(condMap.get("extend") + ""))
		{
			/* 查询扩展表的条件 */
			Map<String, Object> extendMap = new HashMap<String, Object>();
			for (Iterator<AAdmins> iterator = resultList.iterator(); iterator.hasNext();)
			{
				AAdmins admins = (AAdmins) iterator.next();
				/* 根据id查询角色 
				 * 是通过管理员获取角色的id
				 * */
				extendMap.put("id", admins.getRoleId()) ;
				ARole role = this.findOneRoleService(extendMap);
				admins.setRole(role);
			}
		}
		return resultList ; 
	}

	public void setAuthService(IAuthService authService)
	{
		this.authService = authService;
	}
}
